/**
 * 修改个人信息
 * 
 * /general/setdetail
 */

const User_info = require('../../models/usersInfo')
const $ = require('../../utils/common')

const set_detail = async function(req, res) {
    try{
        // user_phone 格式
        if (req.body.user_phone){
            if (!$.isValuebalePhone(req.body.user_phone)) {
                $.sendMessage(res, 1015, '参数user_phone为有效手机号码')
                return
            }
        }

        // user_email 格式
        if (req.body.user_email) {
            if (!$.isValuableEmail(req.body.user_email)) {
                $.sendMessage(res, 1015, '参数user_email为有效邮箱地址')
                return
            }
        }

        delete req.body.user_name
        delete req.body.user_password
        delete req.body.user_role
        delete req.body.id
        delete req.body.user_fullname

        var user_update = await User_info.update(
            req.body,
            {
                where: {
                    user_name: req.user_name
                }   
            }
        )

        // 修改不成功
        if (user_update[0] == 0) {
            $.sendMessage(res, 1016, '修改失败')
            return
        }
        if (user_update[0] == 1) {
            $.sendMessage(res, 200, '修改成功')
            return 
        }

        // 日志
        utils.create_a_log(req, '学生个人信息', '修改')

    }
    catch(err) {
        $.sendMessage(res, 0000, "err:" + err)
        return
    }
}

module.exports = set_detail